//
//  MineDengViewController.m
//  A Learning Walk
//
//  Created by dllo on 16/6/18.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "MineDengViewController.h"
#import "UMSocialSnsPlatformManager.h"
#import "MyButton.h"
#import "HXDLViewController.h"


@interface MineDengViewController ()<UMSocialUIDelegate>

//#import "UMSocialSnsPlatformManager.h"
@property(nonatomic, retain)UIVisualEffectView *effect;//磨玻璃
@end
//@interface MineDengViewController ()


@implementation MineDengViewController

-(void)dealloc{
    [_imageBig release];
    [_imageTou release];
    [_viewA release];
    [_viewB release];
    [_labelW release];
    [_viewQ release];
    [_viewW release];
    [_viewR release];
    [_buttonF release];
    [_effect release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *isLogin = [defaults objectForKey:@"isLogin"];
    if ([isLogin isEqualToString:@"1"]) {
        [self.HXButton setTitle:@"环信登录  (已登录)" forState:UIControlStateNormal];
    }else{
    [self.HXButton setTitle:@"环信登录  (未登录)" forState:UIControlStateNormal];
    }
    

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    

    
    
    
    self.imageBig = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"112"]];
    self.imageBig.frame = self.view.frame;
    self.imageBig.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.imageBig];
//磨砂玻璃效果
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.effect = [[UIVisualEffectView alloc] initWithEffect:blur];
    self.effect.frame = CGRectMake(0, 0, self.imageBig.frame.size.width, self.imageBig.frame.size.height);
    self.effect.alpha = 1;
    [self.imageBig addSubview:self.effect];
    self.imageBig.userInteractionEnabled = YES;
    
    [self creatKong];
    // Do any additional setup after loading the view.
}



-(void)creatKong{

    self.buttonF = [FengButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(10, 30, 30, 30) title:nil image:[UIImage imageNamed:@"106"] color:[UIColor clearColor] handler:^(UIButton *sender) {
        
       [self dismissViewControllerAnimated:YES completion:^{
           
       }];
    }];
    [self.imageBig addSubview:self.buttonF];
    
    self.imageTou = [[UIImageView alloc] init];
    self.imageTou.backgroundColor = [UIColor whiteColor];
    self.imageTou.frame = CGRectMake(self.imageBig.frame.size.width/3, self.imageBig.frame.size.height/8, self.imageBig.frame.size.width/3, self.imageBig.frame.size.width/3);
    self.imageTou.layer.masksToBounds = YES;
    self.imageTou.layer.cornerRadius = self.imageBig.frame.size.width/6;
    self.imageTou.alpha = 0.6;
    [self.imageBig addSubview:self.imageTou];
//中间图片
    UIImageView *imageX = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"116"]];
    imageX.frame = CGRectMake(20, 20, self.imageTou.frame.size.width-40, self.imageTou.frame.size.width-40);
    imageX.backgroundColor = [UIColor clearColor];
    imageX.layer.masksToBounds = YES;
    imageX.layer.cornerRadius = (self.imageTou.frame.size.width-40)/2;
    [self.imageTou addSubview:imageX];
    
    self.viewA = [[UIView alloc] initWithFrame:CGRectMake(40, self.imageTou.frame.size.height+self.imageTou.frame.origin.y+40, self.imageBig.frame.size.width/4, 1)];
    self.viewA.backgroundColor = [UIColor whiteColor];
    [self.imageBig addSubview:self.viewA];
//中间文字
    self.viewB = [[UIView alloc] initWithFrame:CGRectMake(self.imageBig.frame.size.width-40-self.imageBig.frame.size.width/4, self.imageTou.frame.size.height+self.imageTou.frame.origin.y+40, self.imageBig.frame.size.width/4, 1)];
    self.viewB.backgroundColor = [UIColor whiteColor];
    [self.imageBig addSubview:self.viewB];
    
    self.labelW = [FengLabel LabelwithFrame:CGRectMake(self.imageBig.frame.size.width/4+40, self.imageTou.frame.size.height+self.imageTou.frame.origin.y+30, self.imageBig.frame.size.width/4,20) Color:[UIColor clearColor] font:17];
    self.labelW.text = @"边走边听";
    self.labelW.textColor = [UIColor whiteColor];
    self.labelW.textAlignment = NSTextAlignmentCenter;
    [self.imageBig addSubview:self.labelW];
//QQ
    self.viewQ = [FengView creatViewFrame:CGRectMake(40, self.labelW.frame.size.height+self.labelW.frame.origin.y + 15, self.imageBig.frame.size.width-80, self.imageBig.frame.size.height/12) withColor:[UIColor colorWithRed:37/255.0 green:152/255.0 blue:205/255.0 alpha:1] withString:@"119" withLabel:@"手机登录" withColorL:[UIColor whiteColor]];
    [self.imageBig addSubview:self.viewQ];
    self.viewQ.layer.masksToBounds = YES;
    self.viewQ.layer.cornerRadius = 5;
    
    UITapGestureRecognizer *tapGesXin = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesXin:)];
    [self.viewQ addGestureRecognizer:tapGesXin];
    
//微博
    self.viewW = [FengView creatViewFrame:CGRectMake(40, self.viewQ.frame.size.height+self.viewQ.frame.origin.y + 15, self.imageBig.frame.size.width-80, self.imageBig.frame.size.height/12) withColor:[UIColor colorWithRed:246/255.0 green:82/255.0 blue:82/255.0 alpha:1] withString:@"118" withLabel:@"使用微博登录" withColorL:[UIColor whiteColor]];
    [self.imageBig addSubview:self.viewW];
    self.viewW.layer.masksToBounds = YES;
    self.viewW.layer.cornerRadius = 5;
    
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes:)];
    [self.viewW addGestureRecognizer:tapGes];
    
//微信
    self.viewR = [FengView creatViewFrame:CGRectMake(40, self.viewW.frame.size.height+self.viewW.frame.origin.y + 15, self.imageBig.frame.size.width-80, self.imageBig.frame.size.height/12) withColor:[UIColor colorWithRed:17/256.0 green:192/256.0 blue:91/256.0 alpha:1] withString:@"117" withLabel:@"使用微信分享" withColorL:[UIColor whiteColor]];
    [self.imageBig addSubview:self.viewR];
    self.viewR.layer.masksToBounds = YES;
    self.viewR.layer.cornerRadius = 5;
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.viewR addGestureRecognizer:tapGesture];
    
// 环信
    self.HXButton = [MyButton buttonWithType:UIButtonTypeCustom];
    self.HXButton.backgroundColor = DSRGBA(255, 162, 0, 1);
    self.HXButton.imageView.image = [UIImage imageNamed:@"btn_mine_icon5"];
//    NSString *str = [NSString stringWithFormat:@"环信登录%@",self.userStr];
//    [self.HXButton setTitle:str forState:UIControlStateNormal];
    [self.HXButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:self.HXButton];
    [self.HXButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.viewR.mas_bottom).offset(15);
        make.left.mas_equalTo(self.viewR);
        make.right.mas_equalTo(self.viewR);
        make.size.mas_equalTo(self.viewR);
        
        
    }];
    [self.HXButton addTarget:self action:@selector(HXButtonAction:) forControlEvents:UIControlEventTouchUpInside];

}


//第一个按钮,微博登录
-(void)tapGesXin:(UITapGestureRecognizer *)noti{
    
    shouViewController *shouVC = [[shouViewController alloc] init];
    [self presentViewController:shouVC animated:YES completion:^{
        
        
    }];
    
}

//微信手势方法
-(void)tapAction:(UITapGestureRecognizer *)noti{
//微信登录
//    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
//    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
//        if (response.responseCode == UMSResponseCodeSuccess) {
//            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary]valueForKey:UMShareToWechatSession];
//            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
//        }
//    });
    
    //微信分享功能
      [UMSocialSnsService presentSnsIconSheetView:self appKey:@"wx4e800b840d82e365" shareText:@"3bee23c7e808035c5e63d416413d13f6" shareImage:[UIImage imageNamed:@"icon-76.png"] shareToSnsNames:[NSArray arrayWithObjects:UMShareToWechatSession,UMShareToWechatTimeline,UMShareToWechatFavorite, nil] delegate:self];
    
   }
//微博登录
-(void)tapGes:(UITapGestureRecognizer *)noti{

    //登录第二步
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {//登录成功
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            
            
            //登录第三步 ：在登录成功后 获取用户信息
            
            //获取accestoken以及新浪用户信息，得到的数据在回调Block对象形参respone的data属性
            [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToSina  completion:^(UMSocialResponseEntity *response){
                NSLog(@"SnsInformation is %@",response.data);
            }];
            
            
            
        }});
    
}
// 环信登录按钮点击方法
- (void)HXButtonAction:(UIButton *)button{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *isLogin = [defaults objectForKey:@"isLogin"];
    // 判断登录状态如果未登录的情况下点击按钮跳转到登录页面
    if ([isLogin isEqualToString:@"0"]) {
        
        HXDLViewController *HXDLVC = [HXDLViewController new];
        
        [self presentViewController:HXDLVC animated:YES completion:^{
            
            
        }];
        // 如果登录状态下 走这个方法
    }else if ([isLogin isEqualToString:@"1"]){
    
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认退出吗" preferredStyle:UIAlertControllerStyleAlert];
        [alertC addAction:[UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }]];
        [alertC addAction:[UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            // 环信退出登录的方法
            [[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:YES completion:^(NSDictionary *info, EMError *error) {
                if (!error) {
                    NSLog(@"退出成功");
                }
            } onQueue:nil];
            // 将登录状态变为未登录
            [defaults setObject:@"0" forKey:@"isLogin"];
            // 改变登录按钮的文字
            [self.HXButton setTitle:@"环信登录  (未登录)" forState:UIControlStateNormal];
            
        }]];
        [self presentViewController:alertC animated:YES completion:^{
            
            
        }];
    }
    
    
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
